package com.leo.blog.web.resources;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.ResourcesExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.entity.Resources;
import com.leo.blog.enums.ResourcesStateEnum;
import com.leo.blog.exceptions.ResourcesOperationException;
import com.leo.blog.service.ResourcesService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/resources")
public class ResourcesManagementController {
	@Autowired
	private ResourcesService resourcesService;
	
	/**
	 * 通过状态查询资源信息
	 * @return
	 */
	@RequestMapping(value = "/getresourceslistbystatus")
	@ResponseBody
	public Map<String, Object> getResourcesListByStatus(){
		Map<String, Object> modelMap = new HashMap<>();
		ResourcesExecution re = resourcesService.getResourcesListByStatus();
		if(re.getState() == ResourcesStateEnum.SUCCESS.getState()) {
			modelMap.put("list", re.getResourcesList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息失败 ");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getresourceslist")
	@ResponseBody
	public Map<String, Object> getResourcesList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		//空值判断
		if((pageIndex > -1) && (pageSize > -1)) {
			String resourcesTitle = HttpServletRequestUtil.getString(request, "resourcesTitle");
			int status = HttpServletRequestUtil.getInt(request, "status");
			
			Resources resourcesCondition = new Resources();
			//判断是否有条件
			if(resourcesTitle != null && resourcesTitle != "") {
				resourcesCondition.setResourcesTitle(resourcesTitle);
			}
			if(status != 0) {
				resourcesCondition.setStatus(status);
			}
			
			ResourcesExecution re = resourcesService.getResourcesList(resourcesCondition, pageIndex, pageSize);
			
			//返回页数
			int page = 0;
			int pageCount = re.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("resourcesList", re.getResourcesList());
			modelMap.put("count", re.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);		
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getresourcesbyid")
	@ResponseBody
	public Map<String, Object> getResourcesById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//从前台传过来的值
		int resourcesId = HttpServletRequestUtil.getInt(request, "resourcesId");
		
		if(resourcesId > 0) {
			ResourcesExecution re = resourcesService.getResourcesById(resourcesId);
			if(re.getState() == ResourcesStateEnum.SUCCESS.getState()) {
				modelMap.put("resources", re.getResources());
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "查询数据失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteresources")
	@ResponseBody
	public Map<String, Object> deleteResources(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//从前台传过来的值
		int resourcesId = HttpServletRequestUtil.getInt(request, "resourcesId");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			if(resourcesId > 0) {
				ResourcesExecution re = resourcesService.deleteResources("删除资源", resourcesId);
				if(re.getState() == ResourcesStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "删除失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
			
		return modelMap;
	}
	
	@RequestMapping(value = "/modifyresources")
	@ResponseBody
	public Map<String, Object> modifyResources(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//从前台传过来的值
		int resourcesId = HttpServletRequestUtil.getInt(request, "resourcesId");
		String resourcesTitle = HttpServletRequestUtil.getString(request, "resourcesTitle");
		String resourcesContent = HttpServletRequestUtil.getString(request, "resourcesContent");
		String resourcesLink = HttpServletRequestUtil.getString(request, "resourcesLink");
		String password = HttpServletRequestUtil.getString(request, "password");
		int status = HttpServletRequestUtil.getInt(request, "status");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			//创建对象
			Resources resources = new Resources();
			//判断前台传来的是否为空值
			if(resourcesId != 0) {
				resources.setResourcesId(resourcesId);
			}
			if(resourcesTitle != null && resourcesTitle != "") {
				resources.setResourcesTitle(resourcesTitle);
			}
			if(resourcesContent != null && resourcesContent != "") {
				resources.setResourcesContent(resourcesContent);
			}
			if(resourcesLink != null && resourcesLink != "") {
				resources.setResourcesLink(resourcesLink);
			}
			if(password != null && password != "") {
				resources.setPassword(password);
			}
			if(status != 0) {
				resources.setStatus(status);
			}
			
			ResourcesExecution re = new ResourcesExecution();
			//判断id是否为空
			if(resourcesId > 0) {
				re = resourcesService.updateResources("更新资源", resources);
				if(re.getState() == ResourcesStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
			
		return modelMap;
	}
	
	@RequestMapping(value = "/addresources")
	@ResponseBody
	public Map<String, Object> addResources(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//从前台获取传过来的值
		String resourcesTitle = HttpServletRequestUtil.getString(request, "resourcesTitle");
		String resourcesContent = HttpServletRequestUtil.getString(request, "resourcesContent");
		String resourcesLink = HttpServletRequestUtil.getString(request, "resourcesLink");
		String password = HttpServletRequestUtil.getString(request, "password");
		int status = HttpServletRequestUtil.getInt(request, "status");
		
		Bloger bloger = (Bloger)request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			//创建对象
			Resources resources = new Resources();
			//判断前台传来的是否为空值
			if(resourcesTitle != null && resourcesTitle != "") {
				resources.setResourcesTitle(resourcesTitle);
			}
			if(resourcesContent != null && resourcesContent != "") {
				resources.setResourcesContent(resourcesContent);
			}
			if(resourcesLink != null && resourcesLink != "") {
				resources.setResourcesLink(resourcesLink);
			}
			if(password != null && password != "") {
				resources.setPassword(password);
			}
			if(status != 0) {
				resources.setStatus(status);
			}
			
			try {
				//插入数据
				ResourcesExecution re = resourcesService.addResources("添加资源", resources);
				//判断插入是否成功
				if(re.getState() == ResourcesStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "添加资源信息失败");
				}	
			} catch (ResourcesOperationException e) {
				throw new ResourcesOperationException("添加资源信息失败:" + e.getMessage());
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		return modelMap;
	}
}
